$(function() {
	admin.drawItem.init();
});
Namespace('admin.drawItem', {
	init : function() {
		easyui.util.initDatagrid('#datagrid', {
			onDblClickRow : function() {
			}
		});
	},
	query : function() {
		if (!$('#query-form').form('validate'))
			return;
		var data = $('#query-form').form('jsonObject');
		$('#datagrid').datagrid('load', data);
	},
	detail:function(id) {
		if (id != null) {
			top.addTab(id,'详情', contextPath+'/draw/item/detail?id=' + id);
		}
	},
	toAdd:function() {
		top.addTab("1",'添加', contextPath+'/draw/item/toAdd');
	},
	toUpdate:function(id) {
		if (id != null) {
			top.addTab(id,'编辑', contextPath+'/draw/item/toUpdate?id=' + id);
		}
	},
	update: function () {
	 	if(!$('#data-form').form('validate')) return;
        var data=$('#data-form').form('jsonObject');
        var url = contextPath + '/draw/item/update';
        $('#datagrid').datagrid('loading');
		$.post(url, data, function (result) {
			 $('#datagrid').datagrid('loaded');
			if (result.code == 1) {
				top.showInfo('编辑成功');
				top.addTab("1",'公告列表', contextPath+'/draw/item/list');
			} else {
				top.showInfo(result.msg);
			}
		});
	},
	add: function () {
		 	if(!$('#data-form').form('validate')) return;
	        var data=$('#data-form').form('jsonObject');
            var url = contextPath + '/draw/item/create';
			$.post(url, data, function (result) {
				$('#datagrid').datagrid('loaded');
				if (result.code == 1) {
					top.showInfo('创建成功');
					top.addTab("",'优惠卷列表', contextPath+'/draw/item/list');
				} else {
					top.showInfo(result.msg);
				}
			});
	},
	edit:function(id) {
		if(id==null) {
			top.showInfo('请选择需要修改的记录!');
			return;
		}
		getDialog('data-dialog').dialog({
			href:contextPath+'/draw/item/edit/'+id,
			title: '修改类别',
		    width: 500,
		    height: 450,
		    buttons:[{
				text:'确定',
				iconCls:'icon-ok',
				handler:function() {
					admin.homemakingType.doEdit();
				}
			},{
				text:'取消',
				iconCls:'icon-cancel',
				handler:function(){
					$('#data-dialog').dialog('close');
				}
			}]
		});
	},
	doEdit:function() {
		if(!$('#data-form').form('validate')) return;
		var data=$('#data-form').form('jsonObject');
		var img = "";
		var imglist = $(".img_imglist img");
		if (imglist && imglist.length > 0) {
			imglist.each(function(){
			    if (img == "") {
			    	img += $(this).attr("src").replace(imgFilePath,"");
			    } else {
			    	img += ";" + $(this).attr("src").replace(imgFilePath,"");
			    }
			});
		}
		data.img = img;
		$.post(contextPath+'/draw/item/update',data,function(result) {
			if(result.code==1) {
				top.showInfo('修改成功!');
			} else {
				if (result.msg) {
					top.showInfo(result.msg);
				} else {
					top.showInfo('修改失败!');
				}
			}
		});
	},
	addImg:function(id){
		getDialog('query-dialog').dialog({
			href:contextPath+'/draw/item/imgUpload',
			title: '选择文件',
		    width: 350,
		    height: 250,
		    buttons:[{
				text:'上传',
				handler:function(){
					uploaderStart(id);
				}
			}]
		});
		
	},
});